package com.sayice.robotconflict.service;

import cn.hutool.core.bean.BeanUtil;
import com.sayice.robotconflict.common.exception.AppException;
import com.sayice.robotconflict.common.exception.ErrorCode;
import com.sayice.robotconflict.dto.req.PlayerReq;
import com.sayice.robotconflict.entity.BagEntity;
import com.sayice.robotconflict.entity.PlayerEntity;
import com.sayice.robotconflict.entity.RobotEntity;
import com.sayice.robotconflict.entity.SocialEntity;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 玩家业务处理
 *
 * @author shubing
 */
@Slf4j
@Service
public class PlayerService {
    @Autowired
    private MongoTemplate mongoTemplate;
    
    public List<PlayerEntity> queryRanking() {
        log.info("PlayerService queryRanking");
        Query query = new Query().with(Sort.by(Sort.Direction.DESC, "winRate")).limit(100);
        return mongoTemplate.find(query, PlayerEntity.class);
    }
    
    /**
     * 根据 ID 查询
     *
     * @param id ID
     */
    public PlayerEntity queryById(String id) {
        log.info("PlayerService queryById id={}", id);
        return mongoTemplate.findById(new ObjectId(id), PlayerEntity.class);
    }
    
    /**
     * 更新对局次数和胜率
     *
     * @param id      玩家ID
     * @param gameNum 对局次数
     * @param winRate 胜率
     */
    public void updateGameNumAndWinRate(String id, int gameNum, int winRate) {
        log.info("PlayerService updateGameNumAndWinRate id={}, gameNum={}, winRate={}", id, gameNum, winRate);
        Query query = Query.query(Criteria.where("_id").is(id));
        Update update = new Update().set("gameNum", gameNum).set("winRate", winRate);
        mongoTemplate.updateFirst(query, update, PlayerEntity.class);
    }
    
    /**
     * 注册
     */
    public void register(PlayerReq playerReq) {
        log.info("PlayerService register playerReq={}", playerReq);
        Query query = Query.query(Criteria.where("name").is(playerReq.getName()));
        if (mongoTemplate.findOne(query, PlayerEntity.class) != null) {
            throw new AppException(ErrorCode.CODE_400, "玩家名已存在");
        }
        PlayerEntity playerEntity = BeanUtil.copyProperties(playerReq, PlayerEntity.class);
        List<RobotEntity> initRobotEntities = mongoTemplate.findAll(RobotEntity.class, "init_robot");
        mongoTemplate.insert(playerEntity);
        mongoTemplate.insert(new BagEntity(playerEntity.getId(), initRobotEntities));
        mongoTemplate.insert(new SocialEntity(playerEntity.getId()));
    }
    
    /**
     * 登录
     */
    public PlayerEntity login(PlayerReq playerReq) {
        log.info("PlayerService login playerReq={}", playerReq);
        Query query = Query.query(Criteria.where("name").is(playerReq.getName())
                .and("password").is(playerReq.getPassword()));
        PlayerEntity playerEntity = mongoTemplate.findOne(query, PlayerEntity.class);
        if (playerEntity == null) {
            throw new AppException(ErrorCode.CODE_400, "玩家名或密码错误");
        }
        return playerEntity;
    }
}
